package com.example.springaidemo.controller.case1;

import com.example.springaidemo.bean.case1.ActFilm;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * 最简单的chat
 *
 * @author stone
 * @date 2025/6/26 16:11
 */
@RestController
@RequestMapping("/case1")
@Slf4j
public class Case1Controller {

    @Resource
    @Qualifier("zhiPuAiChatClient")
    private ChatClient chatClient;


    /**
     * 直接获取结果
     */
    @GetMapping("/chat")
    public String chat(@RequestParam("input") String input) {
        // input=讲个笑话
        return this.chatClient.prompt()
                .user(input)
                .call()
                .content();
    }

    /**
     * 转化实体
     */
    @GetMapping("/entity")
    public List<ActFilm> entity(@RequestParam("input") String input) {
        // input=生成刘德华和刘亦菲的10部电影
        return this.chatClient.prompt()
                .user(input)
                .call()
                .entity(new ParameterizedTypeReference<List<ActFilm>>() {
                });
    }

    /**
     * 流式响应
     */
    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> flux(@RequestParam("input") String input) {
        // input=讲个笑话
        return this.chatClient.prompt()
                .user(input)
                .stream()
                .content();
    }

    /**
     * 动态输入
     */
    @GetMapping(value = "/fluxDynamic", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> fluxDynamic(@RequestParam("input") String input, @RequestParam("name") String name) {
        return this.chatClient.prompt()
                .user(promptUserSpec -> promptUserSpec
                        .text("告诉我中国有多少叫{name}的人")
                        .param("name", name)
                )
                .stream()
                .content();
    }

}
